# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=tiled
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.11.2
pkgrel=1
pkgdesc='A general purpose tile map editor, built to be flexible and easy to use (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url='https://www.mapeditor.org'
msys2_repository_url='https://github.com/mapeditor/tiled'
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
         "${MINGW_PACKAGE_PREFIX}-qt6-svg"
         "${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache"
         "${MINGW_PACKAGE_PREFIX}-shared-mime-info"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-qbs"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-python")
optdepends=("${MINGW_PACKAGE_PREFIX}-python: Python plugin"
            "${MINGW_PACKAGE_PREFIX}-qt6-imageformats: Support for additional image formats (including WebP)"
            "${MINGW_PACKAGE_PREFIX}-qt6-translations")
source=("${msys2_repository_url}/archive/v${pkgver}/$_realname-$pkgver.tar.gz"
        '0001-tiled-1.11.1-no-link-python-fix.patch')
sha256sums=('f00559f7c5f3115e8396386192c8292d2ba6ff7702f1d60b06165c44de54a51b'
            '3c7f440a8305d2fa2cda652e23cfe6e15d985d9e0d20c2c602b3e5180e682bee')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/0001-tiled-1.11.1-no-link-python-fix.patch
}

build() {
  rm -rf build-${MSYSTEM} | true
  cp -r "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]]; then
    _profile=clang
  else
    _profile=mingw
  fi

  if check_option "debug" "y"; then
    _build_type=debug
  else
    _build_type=release
  fi

  export PYTHONHOME="${MINGW_PREFIX}"/bin
  export MSYS2_ARG_CONV_EXCL="qbs.installPrefix:"

  "${MINGW_PREFIX}"/bin/qbs.exe setup-toolchains \
    --type $_profile \
    "${MINGW_PREFIX}"/bin/${CXX}.exe $_profile

  "${MINGW_PREFIX}"/bin/qbs.exe config profiles.${_profile}.moduleProviders.Qt.qmakeFilePaths ${MINGW_PREFIX}/bin/qmake6.exe

  "${MINGW_PREFIX}"/bin/qbs.exe resolve \
    profile:$_profile \
    qbs.installPrefix:"${MINGW_PREFIX}" \
    qbs.buildVariant:${_build_type} \
    modules.Qt.core.lreleaseName:lrelease-qt6 \
    projects.Tiled.windowsLayout:false \
    projects.Tiled.installHeaders:true

  "${MINGW_PREFIX}"/bin/qbs.exe build
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/qbs.exe install --install-root "${pkgdir}"

  for _file in COPYING LICENSE.{APACHE,BSD,GPL}
  do
    install -Dm644 "${_file}" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/${_file}"
  done
}
